//
//  iGesture.m
//  iGesture
//
//  Created by Tai Le on 6/4/13.
//  Copyright (c) 2013 kms. All rights reserved.
//

#import "iSwipeGesture.h"
#import "Utility.h"

#define DEFAULT_ANIMATION_VECLOCITY 1400
#define SLIDING_SLOWEST_SPEED_TIME 0.4

@implementation iSwipeGesture

-(void) initial
{
    _initialX = self.view.frame.origin.x;
    _initialY = self.view.frame.origin.y;
}
#pragma mark - init LR

-(id)initSwipeLRWithAnchorLeftCenter:(CGFloat)anchorLeft anchorRightCenter:(CGFloat)anchorRight
{
    if (self = [super initWithTarget:self action:@selector(updateRecognizerLR:)])
    {
        [self initial];
        _anchorLeftCenter = anchorLeft;
        _anchorRightCenter = anchorRight;
        _anchorBottomCenter = NSNotFound;
        _anchorTopCenter = NSNotFound;
        
    }
    return self;
}

-(id)initSwipeLRWithAnchorLeftCenter:(CGFloat)anchorLeft
{
    return [self initSwipeLRWithAnchorLeftCenter:anchorLeft anchorRightCenter:NSNotFound];
}

-(id)initSwipeLRWithAnchorRightCenter:(CGFloat)anchorRight
{
    return [self initSwipeLRWithAnchorLeftCenter:NSNotFound anchorRightCenter:anchorRight];
}

-(id)initSwipeLR
{
    return [self initSwipeLRWithAnchorLeftCenter:NSNotFound anchorRightCenter:NSNotFound];
}

#pragma mark - init TD

-(id)initSwipeUDWithAnchorTopCenter:(CGFloat)anchorTop anchorBottomCenter:(CGFloat)anchorBottom
{
    if (self = [super initWithTarget:self action:@selector(updateRecognizerUD:)])
    {
        [self initial];
        _anchorLeftCenter = NSNotFound;
        _anchorRightCenter = NSNotFound;
        _anchorBottomCenter = anchorBottom;
        _anchorTopCenter = anchorTop;
    }
    return self;
}

-(id)initSwipeUDWithAnchorBottomCenter:(CGFloat)anchorBottom
{
    return [self initSwipeUDWithAnchorTopCenter:NSNotFound anchorBottomCenter:anchorBottom];
}

-(id)initSwipeUDWithAnchorTopCenter:(CGFloat)anchorTop
{
    return [self initSwipeUDWithAnchorTopCenter:anchorTop anchorBottomCenter:NSNotFound];
}

-(id)initSwipeUD
{
    return [self initSwipeUDWithAnchorTopCenter:NSNotFound anchorBottomCenter:NSNotFound];
}

#pragma mark - function

- (CGFloat)anchorLeftCenter
{
    if (_anchorLeftCenter == NSNotFound)
    {
        return (_initialX + self.view.bounds.size.width / 2);
    }
    return _anchorLeftCenter;
}

- (CGFloat)anchorRightCenter
{
    if (_anchorRightCenter == NSNotFound)
    {
        return (_initialX + self.view.bounds.size.width * 1.5);
    }
    return _anchorRightCenter;
}

- (CGFloat)anchorTopCenter
{
    if (_anchorTopCenter == NSNotFound)
    {
        return (_initialY + self.view.bounds.size.height / 2);
    }
    return _anchorTopCenter;
}

- (CGFloat)anchorBottomCenter
{
    if (_anchorBottomCenter == NSNotFound)
    {
        return (_initialY + self.view.bounds.size.height * 1.5);
    }
    return _anchorBottomCenter;
}

- (void)updateRecognizerLR:(UIPanGestureRecognizer *)recognizer
{
    if (self.view.superview)
    {
        CGPoint currentTouchPoint     = [recognizer locationInView:self.view.superview];
        CGFloat curX = currentTouchPoint.x;
        
        if (recognizer.state == UIGestureRecognizerStateBegan)
        {
            _preTouchX = curX;
        }
        else if (recognizer.state == UIGestureRecognizerStateChanged)
        {
            CGFloat dx = curX - _preTouchX;
            CGFloat newCenterPosition = self.view.center.x + dx;
            if (newCenterPosition < self.anchorLeftCenter)
            {
                newCenterPosition = self.anchorLeftCenter;
            }
            else if(newCenterPosition > self.anchorRightCenter)
            {
                newCenterPosition = self.anchorRightCenter;
            }
            self.view.center = CGPointMake(newCenterPosition, self.view.center.y);
            
            _preTouchX = curX;
        }
        else if (recognizer.state == UIGestureRecognizerStateEnded || recognizer.state == UIGestureRecognizerStateCancelled)
        {
            CGPoint currentVelocityPoint = [recognizer velocityInView:self.view];
            CGFloat currentVelocityX     = currentVelocityPoint.x;
            if (currentVelocityX > 150)
            {
                [self animationToX:self.anchorRightCenter velocity:currentVelocityX];
            }
            else
            {
                [self animationToX:self.anchorLeftCenter velocity:currentVelocityX];
            } 
        }
    }
}

- (void)updateRecognizerUD:(UIPanGestureRecognizer *)recognizer
{
    if (self.view.superview)
    {
        CGPoint currentTouchPoint     = [recognizer locationInView:self.view.superview];
        CGFloat curY = currentTouchPoint.y;
        
        if (recognizer.state == UIGestureRecognizerStateBegan)
        {
            _preTouchY = curY;
        }
        else if (recognizer.state == UIGestureRecognizerStateChanged)
        {
            CGFloat dy = curY - _preTouchY;
            CGFloat newCenterPosition = self.view.center.y + dy;
            if (newCenterPosition < self.anchorTopCenter)
            {
                newCenterPosition = self.anchorTopCenter;
            }
            else if(newCenterPosition > self.anchorBottomCenter)
            {
                newCenterPosition = self.anchorBottomCenter;
            }
            self.view.center = CGPointMake(self.view.center.x, newCenterPosition);
            
            _preTouchY = curY;
        }
        else if (recognizer.state == UIGestureRecognizerStateEnded || recognizer.state == UIGestureRecognizerStateCancelled)
        {
            CGPoint currentVelocityPoint = [recognizer velocityInView:self.view];
            CGFloat currentVelocityY     = currentVelocityPoint.y;
            if (currentVelocityY > 150)
            {
                [self animationToY:self.anchorBottomCenter velocity:currentVelocityY];
            }
            else
            {
                [self animationToY:self.anchorTopCenter velocity:currentVelocityY];
            }
        }
    }
}

- (void) animationToX:(CGFloat) x velocity:(CGFloat) vel
{
    
    CGFloat distant = abs(self.view.center.x - x);
    CGFloat time = [Utility estimateTimeWithDis:distant andVelocity:abs(vel)];
    [UIView animateWithDuration:time animations:^{
        self.view.center = CGPointMake(x, self.view.center.y);
    } completion:^(BOOL finished) {
        if (x == self.anchorLeftCenter)
        {
            _isAnchorLeft = YES;
            
            if ([_swipeDelegate respondsToSelector:@selector(iSwipeGestureDidAnchorLeft:)])
            {
                [_swipeDelegate iSwipeGestureDidAnchorLeft:self];
            }
        }
        else
        {
            if ([_swipeDelegate respondsToSelector:@selector(iSwipeGestureDidAnchorRight:)])
            {
                [_swipeDelegate iSwipeGestureDidAnchorRight:self];
            }
            
            _isAnchorLeft = NO;
        }
    }];
}

- (void) animationToY:(CGFloat) y velocity:(CGFloat) vel
{
    CGFloat distant = abs(self.view.center.y - y);
    CGFloat time = [Utility estimateTimeWithDis:distant andVelocity:abs(vel)];
    [UIView animateWithDuration:time animations:^{
        self.view.center = CGPointMake(self.view.center.x, y);
    } completion:^(BOOL finished) {
        if (y == self.anchorTopCenter)
        {
            if ([_swipeDelegate respondsToSelector:@selector(iSwipeGestureDidAnchorTop:)])
            {
                [_swipeDelegate iSwipeGestureDidAnchorTop:self];
            }
        }
        else
        {
            if ([_swipeDelegate respondsToSelector:@selector(iSwipeGestureDidAnchorBottom:)])
            {
                [_swipeDelegate iSwipeGestureDidAnchorBottom:self];
            }
        }
    }];
}

- (void)animationToLeft
{
    [self animationToX:self.anchorLeftCenter velocity:DEFAULT_ANIMATION_VECLOCITY];
}

- (void)animationToRight
{
    [self animationToX:self.anchorRightCenter velocity:DEFAULT_ANIMATION_VECLOCITY];
}
@end
